package pl.mcnova.view.panels;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;

import pl.mcnova.model.dao.MailDAO;
import pl.mcnova.model.entity.Mail;
import pl.mcnova.model.table.MailTableModel;
import pl.mcnova.view.dialogs.MailDialog;
import pl.mcnova.view.filters.MailFilter;

public class MailPanel extends JPanel {

	private MailTableModel model;
	private JScrollPane scrollPane;
	private JTable table;
	private MailDAO mailDAO;
	private TableRowSorter<MailTableModel> sorter;
	private MailDialog dialog;

	public MailPanel(String name) {
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(name),
				BorderFactory.createEmptyBorder(0, 6, 6, 6)));

		mailDAO = new MailDAO();
		dialog = new MailDialog();
		model = new MailTableModel(mailDAO.getAll());
		table = new JTable(model);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

		scrollPane = new JScrollPane(table);

		sorter = new TableRowSorter<MailTableModel>(model);
		table.setRowSorter(sorter);

		add(scrollPane, BorderLayout.CENTER);
	}

	public ListSelectionModel getTable() {
		return table.getSelectionModel();
	}

	public int getSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return -1;
		else {
			if (sorter != null) return sorter.convertRowIndexToModel(selected[0]);
			return selected[0];
		}

	}

	public Mail getSelectedMail() {
		return model.get(getSelected());
	}

	public void runFilter(String producer, String model) {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(2);
		filters.add(RowFilter.regexFilter(producer, 1));
		filters.add(RowFilter.regexFilter(model, 2));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

	public int[] getAllSelected() {
		int[] selected = table.getSelectedRows();

		if (selected == null || selected.length == 0)
			return null;
		else {
			if (sorter != null) for (int i = 0; i < selected.length; i++)
				selected[i] = sorter.convertRowIndexToModel(selected[i]);

			return selected;
		}
	}

	public void clearSelection() {
		table.getSelectionModel().removeSelectionInterval(table.getSelectionModel().getMinSelectionIndex(),
				table.getSelectionModel().getMaxSelectionIndex());
	}

	public void add(Mail m) {
		model.add(m);
	}

	public void add(List<Mail> mails) {
		model.add(mails);
	}

	public Mail getMail(int index) {
		return model.get(index);
	}

	public void remove(Mail m) {
		model.remove(m);
	}

	public void runFilter(String subject, String contractor, Long min, Long max) {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		filters.add(RowFilter.regexFilter("(?i)" + contractor, 1));
		filters.add(RowFilter.regexFilter("(?i)" + subject, 2));

		filters.add(new MailFilter(min, max));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

	public void runFilter() {

		List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>();

		filters.add(RowFilter.regexFilter("", 1));
		filters.add(RowFilter.regexFilter("", 2));

		RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
		sorter.setRowFilter(rf);
	}

}
